package com.sky.mapper;

import com.sky.entity.AddressBook;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 地址簿功能数据层
 */
@Mapper
public interface AddressBookMapper {

    /**
     * 根据用户id查询对应的地址
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId}")
    List<AddressBook> list(Long userId);

    /**
     * 添加地址簿地址
     * @param addressBook
     */
    void save(AddressBook addressBook);

    /**
     * 根据用户id查询默认地址
     * @param userId
     * @return
     */
    @Select("select * from address_book where user_id = #{userId} and is_default = 1")
    AddressBook getDefaultAddress(Long userId);

    /**
     * 修改地址信息
     * @param addressBook
     */
    void update(AddressBook addressBook);

    /**
     * 根据id删除地址
     * @param id
     * @param id
     */
    @Select("select * from address_book where id = #{id}")
    AddressBook getById(Long id);

    /**
     * 根据id删除地址
     * @param id
     * @param userId
     */
    @Delete("delete from address_book where id = #{id} and user_id = #{userId}")
    void deleteById(Long id, Long userId);
}
